<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/uDirector.class.php');
require_once(__PHY_LIB.'/class/uWork.class.php');

$rReq = validate($_REQUEST, 'msg:u|action:e[list,edit,new,add,update,delete]');
$sMsg = $rReq['msg'];
$sAction = $rReq['action'];

if ('add' == $sAction) {
	$oOne = new uDirector();
	$rP = validate($_POST, 'title:u|work_1_id:n|work_2_id:n|work_3_id:n|description:u');
	foreach ($rP as $sField => $xValue) {
		$oOne->set($sField, $xValue);
	}
	$oOne->set('time_added', date('Y-m-d H:i:s'));
	if ($s = $oOne->invalid()) {
		$sMsg .= "参数不规范: {$s}".$_br;
	}
	elseif ($oOne->insert()) {
		if ($_FILES['file_thumb']['tmp_name'] && $oOne->saveThumb($_FILES['file_thumb']['tmp_name'])) {
			$oOne->set('thumb', 1);
			$oOne->update();
			$sMsg .= '图片上传成功。'.$_br;
		}
		$sMsg .= '添加成功。'.$_br;
	}
	else {
		$sMsg .= '添加失败。'.$_br;
	}
	$sAction = 'list';
}
elseif ('update' == $sAction) {
	$oOne = new uDirector($_POST['id']);
	$rP = validate($_POST, 'title:u|work_1_id:n|work_2_id:n|work_3_id:n|description:u');
	foreach ($rP as $sField => $xValue) {
		$oOne->set($sField, $xValue);
	}
	if (!(0 < $_POST['id'])) {
		$sMsg .= "没有ID无法修改。".$_br;
	}
	elseif ($s = $oOne->invalid()) {
		$sMsg .= "参数不规范: {$s}".$_br;
	}
	elseif ($oOne->update()) {
		if ($_FILES['file_thumb']['tmp_name'] && $oOne->saveThumb($_FILES['file_thumb']['tmp_name'])) {
			$oOne->set('thumb', 1);
			$oOne->update();
			$sMsg .= '图片上传成功。'.$_br;
		}
		$sMsg .= '修改成功。'.$_br;
	}
	else {
		$sMsg .= '修改失败'.$_br;
	}
	$sAction = 'list';
}
elseif ('delete' == $sAction) {
	$oOne = new uDirector($_POST['id']);
	if ($oOne->delete()) {
		$sMsg .= '删除成功。'.$_br;
	}
	else {
		$sMsg .= '删除失败'.$_br;
	}
	$sAction = 'list';
}

if ('new' == $sAction) {
	;
}
elseif ('edit' == $sAction) {
	$oOne = new uDirector($_REQUEST['id']);
	$oTpl->assign('rOne', $oOne->getData());
	$oList = new uWorkList();
	$oList->cfgWhere("director_1_id='{$_REQUEST['id']}' OR director_2_id='{$_REQUEST['id']}'");
	$oList->load();
	$oTpl->assign('rSlctWorks', $oList->fetchArray());
}
elseif ('list' == $sAction) {
	$rP = validate($_GET, 'pg:n');
	$oTpl->assign('rP', $rP);
	$sWhere = "TRUE";
	/// 作品列表 ///
	$rPager = array(
		'Sze' => 25,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
		'Bse' => http_build_query(array_slice($rP, 0, -1)),
	);
	$oList = new uDirectorList();
	$oList->cfgWhere($sWhere);
	$rPager['Cnt'] = $oList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oList->cfgOrder("id DESC");
	$oList->load($rPager['Sze']*($rPager['Num'] - 1), $rPager['Sze']);
	while ($oOne = $oList->nextItem()) {
		$oWork = new uWork($oOne->get('work_1_id'), 'title');
		$oOne->set('work_1_title', $oWork->get('title'));
		$oWork = new uWork($oOne->get('work_2_id'), 'title');
		$oOne->set('work_2_title', $oWork->get('title'));
		$oWork = new uWork($oOne->get('work_3_id'), 'title');
		$oOne->set('work_3_title', $oWork->get('title'));
	}
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
